package cn.edu.swust.adressbook;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.util.Date;
import java.util.List;

import cn.edu.swust.adressbook.bean.CallLog;
import cn.edu.swust.adressbook.bean.Contact;
import cn.edu.swust.adressbook.bean.Group;
import cn.edu.swust.adressbook.service.IBaseService;
import cn.edu.swust.adressbook.service.impl.CallLogServiceImpl;
import cn.edu.swust.adressbook.service.impl.ContactServiceImpl;
import cn.edu.swust.adressbook.service.impl.GroupServiceImpl;

/**
 * 用法实例
 */

public class MainActivity extends AppCompatActivity {

    /**
     * 用接口定义变量，在init方法中初始化
     * 需要传入上下文this 必须在 setContentView(R.layout.activity_main); 之后执行
     * 要什么表 实现对应的实例
     * 记得在 onDestroy 中调用 close
     */

    private IBaseService mContactService;
    private IBaseService mCallLogService;
    private IBaseService mGroupService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        init();
      //  getGroup();
        add();
        insertGroup();
//        delete();
//        getAll();
//        insert();
//        get();
    }

    private void getGroup() {
        List<Group> groups = mGroupService.getAll();
        for (int i = 0; i < groups.size(); i++) {
            Log.i("MainActivity", groups.get(i).getName() + " " + groups.get(i).getContactCount());
        }
    }

    private void init() {
        mContactService = new ContactServiceImpl(this);
        mCallLogService = new CallLogServiceImpl(this);
        mGroupService = new GroupServiceImpl(this);
    }


    private void delete() {
        /**
         * 不支持多个删除 多个删除用for循环 逐个删除
         * 实例必须有ID
         */
        Contact contact = new Contact();
        contact.setId(13L);
        mContactService.delete(contact);

        Group group = new Group();
        group.setId(1L);
        mGroupService.delete(group);

        CallLog callLog = new CallLog();
        callLog.setId(1L);
        mCallLogService.delete(callLog);
    }



    private void getAll() {
        List<CallLog> callLogs = mCallLogService.getAll();
        List<Group> groups = mGroupService.getAll();
        List<Contact> contacts = mContactService.getAll();
    }


    private void get() {
        /**
         * 得到一个实例 通过ID
         *
         */
        CallLog callLog = (CallLog) mCallLogService.get(1L);
        Contact contact = (Contact) mContactService.get(1L);
        Group group = (Group) mGroupService.get(1L);
    }

    private void insert() {
        /**
         * 不要设置ID 自动生成
         * 其他熟悉随意
         */
        Group group = new Group();
        group.setName("777");
        mGroupService.add(group);

        CallLog callLog = new CallLog();
        callLog.setDate(new Date());
        mCallLogService.add(callLog);

        /**
         * 插入人的时候不需要写拼音 后台代码会自动生成
         */
        Contact contact = new Contact();
        contact.setName("张三");
        mContactService.add(contact);

    }

    private void insertGroup(){

        for (int i = 1; i < 15; i++) {
            Group group = new Group();
            group.setName(String.valueOf(i * 100 + i * 10 + i));
            mGroupService.add(group);
        }


    }

    private void add(){
        for (int i = 0; i < 20; i++) {
            Contact contact = new Contact(null,1,"韩鹏" + i, null,"索尼中国","屌丝一枚", (long)i % 4 + 1,"四川遂宁","15892499063","1220123963@qq.com",12);
            mContactService.add(contact);
        }
    }

    private void update(){
        /**
         * 必须设置ID
         * 其他熟悉随意
         */
        Group group = new Group();
        group.setId(1L);
        group.setName("777");
        mGroupService.add(group);

        CallLog callLog = new CallLog();
        callLog.setId(1L);
        callLog.setDate(new Date());
        mCallLogService.add(callLog);

        Contact contact = new Contact();
        contact.setId(1L);
        contact.setName("张三");
        mContactService.add(contact);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mContactService.close();
    }
}
